<?php
namespace app\admin\controller;



use app\common\model\PtGroup;

class Order extends Base
{
    private $m="order";

    public function lists(){
        $status=input("status");
        $where=[];
        $where['isused']=1;
        if(isset($status) && $status!==""){
            $where['status']=$status;
        }
        $list = model($this->m)->where($where)->order("time asc")->paginate(10,null,['query'=>['status'=>$status]]);
        $this->assign("list",$list);
        return view();
    }

    public function edit(){
        $id=input("id");
        $oldnumber=0;
        if(!empty($id)){
            $info=model($this->m)->where("id",$id)->find();
            $this->assign("info",$info);
            $oldnumber=$info['number'];
        }
        if(request()->isPost()){
            $data=$this->autodata();
            if(!empty($data['tags_form'])){
                $data['tags']=implode(",",$data['tags_form']);
            }
            $id=input("id");
            if(empty($id)){
                model($this->m)->allowField(true)->save($data);
                $goods_id=model($this->m)->id;
            }else{
                model($this->m)->allowField(true)->save($data,["id"=>$id]);
                $goods_id=$id;
            }
            $this->inventory($goods_id,$data['cnumber']-$oldnumber,"管理员变更");
            $this->success("编辑成功","/admin/{$this->m}/lists");
        }
        return view();

    }

    public function del(){
        $id=input("id");
        $mod=model($this->m)->where("id",$id)->find();
        if(empty($mod)){
            $this->error("数据不存在");
        }
        $mod->save(['isdelete'=>0]);
        $this->success("删除成功","/admin/{$this->m}/lists");
    }
    public function detail(){
        $orderno=i("orderno","请输入订单号");

        $order = model("order")->where("orderno",$orderno)->find();
        $goodslist = model('orderGoodsListView')->where('orderno',$order['orderno'])->select();
        $orderprice = model('orderPriceList')->where('orderno',$order['orderno'])->select();
        $exp_no = model("exp")->where("no",$order['exp_no'])->find();
        $this->assign('exp_no',$exp_no);
        $exp = model("exp")->select();
        $this->assign('exp',$exp);
        $this->assign("orderprice",$orderprice);
        $this->assign("order",$order);
        $this->assign("goodslist",$goodslist);
        return view();
    }
    //发货
    public function deliver(){
        $orderno = i('orderno');
        $exp_no = i('select');
        if (empty($exp_no)){
            $this->error('请选择快递公司!');
        }
        $exp_order = i('exp_no');
        if (empty($exp_order)){
            $this->error('请填写快递单号!');
        }
        $order=model('order')->where('orderno',$orderno)->find();
        if(empty($order)){
            $this->error("订单不存在");
        }
        if(intval($order['status'])!==1){
            $this->error("订单状态不允许发货");
        }
        model('order')->where('orderno',$orderno)->update(['exp_no'=>$exp_no,'exp_order'=>$exp_order,"status"=>2]);
        $this->success('发货成功!');
        return view();
    }
    //点击退款
    public function refunds(){
        $orderno = $_POST['orderno'];
        $order = model('order')->where('orderno',$orderno)->find();
        if (empty($order)){
            return 404;
       }
        if($order['status']==4){
            return 500;
        }
        if (model('order')->where('orderno',$orderno)->update(['status'=>4])){
            return 66;
        }
        return 4;
    }
    //查询快递位置
    public function lookexpress(){
        $orderno = $_POST['orderno'];
        $order=model("order")->where("orderno",$orderno)->find();
        if(empty($order['exp_no'])||empty($order['exp_order'])){
            e("快递单号不存在");
        }

        $host = "https://wuliu.market.alicloudapi.com";//api访问链接
        $path = "/kdi";//API访问后缀
        $method = "GET";
        $appcode = "4afed0b5c6e3437fbb4320210e410c2c";//替换成自己的阿里云appcode
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        $querys = "no={$order['exp_order']}&type={$order['exp_no']}";  //参数写在这里
        $bodys = "";
        $url = $host . $path . "?" . $querys;//url拼接

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $data=json_decode(curl_exec($curl),true);
        if($data['status']==0){
            return json($data['result']['list']);
        }else{
            return json([]);
        }
    }
    //订单完成
    public function done(){
        $orderno = $_POST['orderno'];
        $order = model('order')->where('orderno',$orderno)->find();
        if (empty($order)){
            return 2;
        }
        $order = model('order')->where('orderno',$orderno)->update(['status'=>3]);
        return 1;
    }
}
